<?php

/**
 * Radcodes - SocialEngine Module
 *
 * @category   Application_Extensions
 * @package    Cmspage
 * @copyright  Copyright (c) 2009-2010 Radcodes LLC (http://www.radcodes.com)
 * @license    http://www.radcodes.com/license/
 * @version    $Id$
 * @author     Vincent Van <vincent@radcodes.com>
 */

class Cmspage_Model_Cmspage extends Core_Model_Item_Abstract {
	// Properties
	

	protected $_parent_type = 'user';
	
	protected $_searchColumns = array ('title', 'description' );
	
	protected $_parent_is_owner = true;
	
	/**
	 * Gets an absolute URL to the page to view this item
	 *
	 * @return string
	 */
	public function getHref($params = array()) {
		$slug = $this->getSlug ();
		
		$params = array_merge ( array ('route' => 'cmspage_profile', 'reset' => true, // 'user_id' => $this->owner_id,
		'cmspage_id' => $this->cmspage_id, 'slug' => $slug ), $params );
		$route = $params ['route'];
		$reset = $params ['reset'];
		unset ( $params ['route'] );
		unset ( $params ['reset'] );
		return Zend_Controller_Front::getInstance ()->getRouter ()->assemble ( $params, $route, $reset );
	}
	
	public function getSlug() {
		return Radcodes_Lib_Helper_Text::slugify ( $this->getTitle () );
	}
	
	public function getExcerpt($length = 164, $truncate_string = '...', $truncate_lastspace = false) {
		$text = strip_tags ( $this->description );
		return Radcodes_Lib_Helper_Text::truncate ( $text, $length, $truncate_string, $truncate_lastspace );
	}
	
	public function getKeywords($separator = ' ') {
		$keywords = array ();
		foreach ( $this->tags ()->getTagMaps () as $tacmspage ) {
			$tag = $tacmspage->getTag ();
			$keywords [] = $tag->getTitle ();
		}
		
		if (null === $separator) {
			return $keywords;
		}
		
		return join ( $separator, $keywords );
	}
	
	public function setPhoto($photo) {
		$viewer = Engine_Api::_ ()->user ()->getViewer ();
		$cmspageAlbum = $this->getSingletonAlbum ();
		
		$params = array ('collection_id' => $cmspageAlbum->getIdentity (), 'album_id' => $cmspageAlbum->getIdentity (), 'cmspage_id' => $this->getIdentity (), 'user_id' => $viewer->getIdentity () );
		
		$photoItem = Engine_Api::_ ()->cmspage ()->createPhoto ( $params, $photo );
		
		// Update row
		$this->modified_date = date ( 'Y-m-d H:i:s' );
		$this->photo_id = $photoItem->file_id;
		$this->save ();
		
		return $this;
	
	}
	
	public function getPhoto($photo_id) {
		$photoTable = Engine_Api::_ ()->getItemTable ( 'cmspage_photo' );
		$select = $photoTable->select ()->where ( 'file_id = ?', $photo_id )->limit ( 1 );
		
		$photo = $photoTable->fetchRow ( $select );
		return $photo;
	}
	
	public function getSingletonAlbum() {
		$table = Engine_Api::_ ()->getItemTable ( 'cmspage_album' );
		$select = $table->select ()->where ( 'cmspage_id = ?', $this->getIdentity () )->order ( 'album_id ASC' )->limit ( 1 );
		
		$album = $table->fetchRow ( $select );
		
		if (null === $album) {
			$album = $table->createRow ();
			$album->setFromArray ( array ('title' => $this->getTitle (), 'cmspage_id' => $this->getIdentity () ) );
			$album->save ();
		}
		
		return $album;
	}
	
	public function isPublished() {
		return $this->published ? true : false;
	}
	
	// Interfaces
	/**
	 * Gets a proxy object for the comment handler
	 *
	 * @return Engine_ProxyObject
	 **/
	public function comments() {
		return new Engine_ProxyObject ( $this, Engine_Api::_ ()->getDbtable ( 'comments', 'core' ) );
	}
	
	/**
	 * Gets a proxy object for the like handler
	 *
	 * @return Engine_ProxyObject
	 **/
	public function likes() {
		return new Engine_ProxyObject ( $this, Engine_Api::_ ()->getDbtable ( 'likes', 'core' ) );
	}
	
	/**
	 * Gets a proxy object for the tags handler
	 *
	 * @return Engine_ProxyObject
	 **/
	public function tags() {
		return new Engine_ProxyObject ( $this, Engine_Api::_ ()->getDbtable ( 'tags', 'core' ) );
	}
	
	/**
	 * 
	 * @return Cmspage_Model_Category
	 */
	public function getCategory() {
		if (! $this->category_id)
			return null;
		
		//$category = Engine_Api::_()->getItem('cmspage_category', $this->category_id);
		

		$category = Engine_Api::_ ()->cmspage ()->getCategory ( $this->category_id );
		
		return $category;
	}
	
	protected function _delete() {
		if ($this->_disableHooks)
			return;
		
		// Delete all albums
		$albumTable = Engine_Api::_ ()->getItemTable ( 'cmspage_album' );
		$albumSelect = $albumTable->select ()->where ( 'cmspage_id = ?', $this->getIdentity () );
		foreach ( $albumTable->fetchAll ( $albumSelect ) as $cmspageAlbum ) {
			$cmspageAlbum->delete ();
		}
		
		$values = Engine_Api::_ ()->fields ()->getFieldsValues ( $this );
		foreach ( $values as $value ) {
			$value->delete ();
		}
		
		$search = Engine_Api::_ ()->fields ()->getFieldsSearch ( $this );
		if ($search) {
			$search->delete ();
		}
		
		$tagmaps = $this->tags ()->getTagMaps ();
		foreach ( $tagmaps as $tagmap ) {
			$tagmap->delete ();
		}
		
		parent::_delete ();
	}
}